package com.debao;

import cn.hutool.core.date.DateUtil;
import com.debao.common.annotation.Anonymous;
import com.debao.common.constant.Constants;
import com.debao.common.core.domain.AjaxResult;
import com.debao.common.utils.SecurityUtils;
import com.debao.system.domain.*;
import com.debao.system.service.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static com.debao.common.core.domain.AjaxResult.error;
import static com.debao.common.core.domain.AjaxResult.success;

/**
 * 场馆
 */
@RestController
@RequestMapping("/api/stadium")
public class ApiStadium {

    @Resource
    private IStadiumService stadiumService;

    @Resource
    private IStadiumTableService stadiumTableService;

    @Resource
    private IStadiumTableScheduleService scheduleService;

    @Resource
    private ICoachService coachService;

    @Resource
    private ICollectService collectService;

    // 锁定
    private static final Integer unable = 0;

    /**
     * 场馆列表
     *
     * @return
     */
    @GetMapping("/list")
    @Anonymous
    public AjaxResult stadiumList() {
        return success(stadiumService.selectStadiumList(new Stadium().setStatus(Constants.STATUS_1)));
    }

    /**
     * 场馆详情
     *
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    @Anonymous
    public AjaxResult coachList(@PathVariable Long id) {
        return success(
                new HashMap<String, Object>() {{
                    // 场馆信息
                    put("info", stadiumService.selectStadiumById(id));
                    // 教练信息
                    put("coachList", coachService.selectCoachList(new Coach().setStadiumId(id).setIsDel(Constants.IS_DEL_N).setStatus(Constants.STATUS_1)));
                    // 场馆的桌台
                    put("stadiumTableList", stadiumTableService.selectStadiumTableList(new StadiumTable().setStadiumId(id)));
                }});
    }


    /**
     * 获取桌台排期
     *
     * @param tableId date
     * @return
     */
    @GetMapping("/schedule/{tableId}/{date}")
    @Anonymous
    public AjaxResult scheduleList(@PathVariable Long tableId, @PathVariable String date) {
        // 当前时间整点
        String time = "";
        String toDay = DateUtil.format(new Date(),"yyyy-MM-dd");
        if(toDay.equals(date)){
            time = DateUtil.format(new Date(),"HH:00");
        }
        return success(scheduleService.selectStadiumTableScheduleList(new StadiumTableSchedule().setStadiumTableId(tableId).setDate(date).setNowTime(time)));
    }

    /**
     * 预约桌台
     *
     * @return
     */
    @PostMapping("schedule/appoint")
    public AjaxResult appointSchedule(@RequestParam String tableScheduleIds) {
        Long userId = SecurityUtils.getUserId();
        List<String> idList = Arrays.asList(tableScheduleIds.split(","));
        idList.forEach(id -> {
            scheduleService.updateStadiumTableSchedule(new StadiumTableSchedule()
                    .setId(Long.parseLong(id))
                    .setUsable(unable)
                    .setAppointUserId(userId)
                    .setAppointAt(new Date()));
        });
        return success();
    }

    /**
     * 收藏场馆
     */
    @PostMapping(value = "addCollect/{id}")
    public AjaxResult addCollect(@PathVariable("id") Long id) {
        int row = collectService.insertCollect(new Collect().setUserId(SecurityUtils.getUserId()).setOtherId(id).setType("S"));
        return row == 1 ? success("收藏成功") : error("已收藏");
    }

    /**
     * 取消收藏
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "delCollect/{id}")
    public AjaxResult delCollect(@PathVariable("id") Long id) {
        collectService.deleteCollect(SecurityUtils.getUserId(), id, "S");
        return success("已取消收藏");
    }

}
